package com.lhh.userservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.Collect;
import com.lhh.springcommon.entity.vo.VideoVO;
import com.lhh.springcommon.mapper.CollectMapper;
import com.lhh.springcommon.service.ICollectService;
import com.lhh.springcommon.utils.PageQuery;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.userservice.service.UserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 李厚航
 * @date 2025-03-01 22:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserCollectServiceImpl implements UserCollectService {

    private final ICollectService collectService;

    private final CollectMapper collectMapper;

    @Autowired
    public UserCollectServiceImpl(ICollectService collectService, CollectMapper collectMapper) {
        this.collectService = collectService;
        this.collectMapper = collectMapper;
    }

    @Override
    @WebLog
    public Long countCollect(Integer uid,Integer vid) {
        return collectService.count(new LambdaQueryWrapper<Collect>()
                .eq(uid!=null,Collect::getUid, uid)
                .eq(vid!=null,Collect::getVid, vid));
    }

    @Override
    @WebLog
    @Cacheable(value = "queryPageCollectVideo", key = "#page + ':' + #pageSize + ':' + #uid + ':' + #vid")
    public Page<VideoVO> queryPageCollectVideo(Integer page,Integer pageSize,Integer uid,Integer vid) {
        Collect collect = new Collect();
        collect.setUid(uid);
        collect.setVid(vid);
        return collectMapper.queryPageCollectVideo(collect, new Page<>(page, pageSize));
    }

    @Override
    @WebLog
    @CacheEvict(value = "queryPageCollectVideo",allEntries = true)
    public Boolean cancelCollect(Collect collect) {
        return collectService.remove(new LambdaQueryWrapper<Collect>()
                .eq(collect.getId()!=null,Collect::getId, collect.getId())
                .eq(collect.getUid() != null,Collect::getUid, collect.getUid())
                .eq(collect.getVid() != null,Collect::getVid, collect.getVid()));
    }

    @Override
    @WebLog
    @CacheEvict(value = "queryPageCollectVideo",allEntries = true)
    public Boolean collect(Collect collect) {
        if(collectService.getOne(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUid,collect.getUid())
                .eq(Collect::getVid,collect.getVid())) == null){
            return collectService.save(collect);
        }
        return false;
    }

    @Override
    @WebLog
    public Boolean isCollect(Integer uid,Integer vid) {
        return collectService.getOne(new LambdaQueryWrapper<Collect>()
                .eq(uid != null,Collect::getUid, uid)
                .eq(vid != null,Collect::getVid, vid)) != null;
    }
}
